<template>
  <div class="container" ref="box">
    <div class="Interval-container">
      <div class="Interval-title">
        <div style="">统计方案</div>
        <el-switch
          v-model="isTwo"
          active-text="对比统计方案"
          active-color="#13ce66">
        </el-switch>
      </div>
      <div class="Interval-main">
        <interval-param ref="IntervalParamA1" title="A检间隔参数" ></interval-param>
        <interval-param ref="IntervalParamC1" title="C检间隔参数" ></interval-param>
        <interval-param ref="IntervalParamA2" title="A检间隔参数" :isDisabled="!isTwo"></interval-param>
        <interval-param ref="IntervalParamC2" title="C检间隔参数" :isDisabled="!isTwo"></interval-param>
      </div>
    </div>

    <div class="time-container">
      <time-param ref="TimeParam" title="接近工时参数"></time-param>
    </div>

    <div class="buttons-container">
      <el-button type="info" plain size="medium" v-loading.fullscreen.lock="Loading1" element-loading-text="数据加载中" @click="upMTOP(); upMTOP2()">工时计算</el-button>
      <el-dropdown @command="handleCommandMTOP">
        <el-button type="info" plain size="medium" :disabled="isChecked">导出MTOP表</el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="1">导出MTOP表1</el-dropdown-item>
          <el-dropdown-item v-if="isTwo" command="2">导出MTOP表2</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-dropdown @command="handleCommandMTOP">
        <el-button type="info" plain size="medium" :disabled="isChecked">导出工时表</el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="3">导出工时表1</el-dropdown-item>
          <el-dropdown-item v-if="isTwo" command="4">导出工时表2</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    
    <div class="table-container">
      <bar-chart class="bar-chart" :key="chart_key1 + '1'" title="方案统计一" :x_data="x_data_1" :access_time="access_time_1" :work_time="work_time_1"></bar-chart>
      <bar-chart v-if="isTwo" class="bar-chart" :key="chart_key2 + '2'" title="方案统计二" :x_data="x_data_2" :access_time="access_time_2" :work_time="work_time_2"></bar-chart>
      <div class="table-item">
        <table border="" cellspacing="0" cellpadding="0">
          <tr>
            <th colspan="2">分析区间</th>
            <th style="width: 200px">方案1</th>
            <th style="width: 200px">方案2</th>
            <th style="width: 200px" rowspan="3">差值</th>
          </tr>
          <tr>
            <th colspan="2">A检</th>
            <th>{{AworkLength1}}个</th>
            <th>{{AworkLength2}}个</th>
          </tr>
          <tr>
            <th colspan="2">C检</th>
            <th>{{CworkLength1}}个</th>
            <th>{{CworkLength2}}个</th>
          </tr>
          <tr>
            <th rowspan="4" style="width: 150px">A检工时</th>
            <th style="width: 150px">工作</th>
            <td>{{AworkSum_1}}</td>
            <td>{{AworkSum_2}}</td>
            <td>{{(AworkSum_1 - AworkSum_2).toFixed(2)}}</td>
          </tr>
          <tr>
            <th>接近</th>
            <td>{{AaccessSum_1}}</td>
            <td>{{AaccessSum_2}}</td>
            <td>{{(AaccessSum_1 - AaccessSum_2).toFixed(2)}}</td>
          </tr>
          <tr>
            <th>总</th>
            <td>{{ASum_1}}</td>
            <td>{{ASum_2}}</td>
            <td>{{(ASum_1 - ASum_2).toFixed(2)}}</td>
          </tr>
          <tr>
            <th>平均</th>
            <td>{{AValue1.toFixed(2)}}</td>
            <td>{{AValue2.toFixed(2)}}</td>
            <td>{{(AValue1 - AValue2).toFixed(2)}}</td>
          </tr>
          <tr>
            <th rowspan="4">C检工时</th>
            <th>工作</th>
            <td>{{CworkSum_1}}</td>
            <td>{{CworkSum_2}}</td>
            <td>{{(CworkSum_1 - CworkSum_2).toFixed(2)}}</td>
          </tr>
          <tr>
            <th>接近</th>
            <td>{{CaccessSum_1}}</td>
            <td>{{CaccessSum_2}}</td>
            <td>{{(CaccessSum_1 - CaccessSum_2).toFixed(2)}}</td>
          </tr>
          <tr>
            <th>总</th>
            <td>{{CSum_1}}</td>
            <td>{{CSum_2}}</td>
            <td>{{(CSum_1 - CSum_2).toFixed(2)}}</td>
          </tr>
          <tr>
            <th>平均</th>
            <td>{{CValue1.toFixed(2)}}</td>
            <td>{{CValue2.toFixed(2)}}</td>
            <td>{{(CValue1 - CValue2).toFixed(2)}}</td>
          </tr>
          <tr>
            <th rowspan="4">总检工时</th>
            <th>工作</th>
            <td>{{(AworkSum_1 + CworkSum_1).toFixed(2)}}</td>
            <td>{{(AworkSum_2 + CworkSum_2).toFixed(2)}}</td>
            <td>{{(AworkSum_1 + CworkSum_1 - AworkSum_2 - CworkSum_2).toFixed(2)}}</td>
          </tr>
          <tr>
            <th>接近</th>
            <td>{{(CaccessSum_1 + AaccessSum_1).toFixed(2)}}</td>
            <td>{{(CaccessSum_2 + AaccessSum_2).toFixed(2)}}</td>
            <td>{{(CaccessSum_1 + AaccessSum_1 - CaccessSum_2 - AaccessSum_2).toFixed(2)}}</td>
          </tr>
          <tr>
            <th>总</th>
            <td>{{(ASum_1 + CSum_1).toFixed(2)}}</td>
            <td>{{(ASum_2 + CSum_2).toFixed(2)}}</td>
            <td>{{(ASum_1 + CSum_1 - ASum_2 - CSum_2).toFixed(2)}}</td>
          </tr>
          <tr>
            <th>平均</th>
            <td>{{SumValue1.toFixed(2)}}</td>
            <td>{{SumValue2.toFixed(2)}}</td>
            <td>{{(SumValue1 - SumValue2).toFixed(2)}}</td>
          </tr>
        </table>
      </div>
    </div>
  </div>
</template>

<script>
// import store from '../store/index'
// import { mapMutations } from 'vuex'
import { downloadMTOP, updateMTOPOne, updateMTOPTwo, updateWorkHourSchemeOne, updateWorkHourSchemeTwo, fileExist } from '../../api/modules/login'
import router from '../../router/index'
import BarChart from'./barChart'
import IntervalParam from './intervalParam'
import TimeParam from './timeParam'
import echarts from 'echarts'
export default {
  components: {
    BarChart,
    IntervalParam,
    TimeParam
  },
  props: {
  },
  data () {
    return {
      userId: "",
      isTwo: false,
      Loading1: false,

      max_value_A: 3,
      max_value_C: 3,

      isChecked: true,
      isChecked2: true,

      AaccessSum_1: 0,
      AworkSum_1: 0,
      CaccessSum_1: 0,
      CworkSum_1: 0,
      ASum_1: 0,
      CSum_1: 0,

      AaccessSum_2: 0,
      AworkSum_2: 0,
      CaccessSum_2: 0,
      CworkSum_2: 0,
      ASum_2: 0,
      CSum_2: 0,

      chart_key1: 1,
      x_data_1: [],
      access_time_1: [],
      work_time_1: [],

      chart_key2: 1,
      x_data_2: [],
      access_time_2: [],
      work_time_2: [],

      CworkLength1: 0,
      AworkLength1: 0,
      CworkLength2: 0,
      AworkLength2: 0,

      AValue1: 0,
      AValue2: 0,
      CValue1: 0,
      CValue2: 0,
      SumValue1: 0,
      SumValue2: 0,
    }
  },
  watch: {
  },
  mounted () {
    // userId的检测
    if(! window.localStorage.getItem("userId")){  
      let id = parseInt(new Date().getTime()).toString()
      window.localStorage.setItem("userId", id)
      this.userId = id
    }
    else{
      this.userId = window.localStorage.getItem("userId")
    }
    this.checkFile()
  },
  methods: {
    handleCommandMTOP(command){
      if(command === '1'){
        this.downMTOPOne()
      }
      else if(command === '2'){
        this.downMTOPTwo()
      }
      else if(command === '3'){
        this.downWorkHourOne()
      }
      else{
        this.downWorkHourTwo()
      }
    },

    checkFile(){
      fileExist(this.userId).then((res) => {
        if (res.status === 200) {
          if(res.data.errCode == 0){
            this.$store.commit("setFileOk", true)
          }
          else{
            console.log("erro")
            this.$store.commit("setFileOk", false)
            let third = {
              path: '/third'
            }
            router.push(third)
          }
        } else {
          console.log("error ok")
        }
      })
    },
    downMTOPOne () {
      //发送请求并更新数据
      window.open('http://10.112.90.59:8081/ameco/downloadMTOPOne?id=' + this.userId)
      // window.open('/ameco/downloadMTOPOne?id=' + this.userId)
    },
    downMTOPTwo () {
      //发送请求并更新数据
      // window.open('http://10.112.90.59:8081/ameco/downloadMTOPTwo?id=' + this.userId)
      window.open('/ameco/downloadMTOPTwo?id=' + this.userId)
    },
    downWorkHourOne () {
      //发送请求并更新数据
      window.open('http://10.112.90.59:8081/ameco/downloadWorkHourSchemeOne?id=' + this.userId)
      // window.open('/ameco/downloadWorkHourSchemeOne?id=' + this.userId)
    },
    downWorkHourTwo () {
      //发送请求并更新数据
      // window.open('http://10.112.90.59:8081/ameco/downloadWorkHourSchemeTwo?id=' + this.userId)
      window.open('/ameco/downloadWorkHourSchemeTwo?id=' + this.userId)
    },
    upMTOP () {
      this.Loading1 = true
      this.isChecked = false
      let paramA = this.$refs.IntervalParamA1.get_param()
      let paramC = this.$refs.IntervalParamC1.get_param()
      let bodyJson = {
        fh_a: paramA.FH, fc_a: paramA.FC, mo_a: paramA.MO,
        fh_c: paramC.FH, fc_c: paramC.FC, mo_c: paramC.MO,
        }
      updateMTOPOne(bodyJson, this.userId).then((res) => {
        if (res.status === 200) {
          console.log("200 ok")
          this.changeData ()
        } else {
          this.$message.error("error");
        }
      })
    },
    upMTOP2 () {
      if(this.isTwo){
        this.Loading1 = true
        this.isChecked2 = false
        let paramA = this.$refs.IntervalParamA2.get_param()
        let paramC = this.$refs.IntervalParamC2.get_param()
        let bodyJson = {
          fh_a: paramA.FH, fc_a: paramA.FC, mo_a: paramA.MO,
          fh_c: paramC.FH, fc_c: paramC.FC, mo_c: paramC.MO,
          }
        updateMTOPTwo(bodyJson, this.userId).then((res) => {
          if (res.status === 200) {
            console.log("200 ok")
            this.changeData2 ()
          } else {
            this.$message.error("error");
          }
        })
      }
    },
    changeData () {
      let paramA = this.$refs.IntervalParamA1.get_param()
      let paramC = this.$refs.IntervalParamC1.get_param()
      let param = this.$refs.TimeParam.get_param()
      let valueA = Math.min(paramA.FH / param.A, paramA.FC / param.C, paramA.MO *30.4)
      let valueC = Math.min(paramC.FH / param.A, paramC.FC / param.C, paramC.MO *30.4)
      let bodyJson = {
        atime: {year: param.max, value: valueA},
	      ctime: {year: param.max, value: valueC}
      }
      updateWorkHourSchemeOne(bodyJson, this.userId).then((res) => {
        console.log(res)
        if (res.status === 200) {
          this.Loading1 = false
          console.log("200 ok")
          this.AaccessSum_1 = res.data.totalAAccessHour
          this.AworkSum_1 = res.data.totalAWorkHour
          this.ASum_1 = res.data.totalAHour
          this.CaccessSum_1 = res.data.totalCAccessHour
          this.CworkSum_1 = res.data.totalCWorkHour
          this.CSum_1 = res.data.totalCHour

          let listAll = []
          let Acount = res.data.awork.length
          this.AworkLength1 = Acount
          let Ccount = res.data.cwork.length
          this.CworkLength1 = Ccount

          if(Acount != 0) {this.AValue1 = res.data.totalAHour / Acount} else this.AValue1 = 0
          if(Ccount != 0) {this.CValue1 = res.data.totalCHour / Ccount} else this.CValue1 = 0
          if(Acount + Ccount != 0) {this.SumValue1 = (res.data.totalAHour + res.data.totalCHour) / (Acount + Ccount)} else this.SumValue1 = 0

          if(Acount >= Ccount){
            let count = Math.ceil(Acount / Ccount)
            for(let i = 1; i <= Acount; i++){
              if(i % count == 0){
                let cdata = res.data.cwork[i / count - 1]
                cdata.number = cdata.number + "C"
                listAll.push(cdata)
              }
              let adata = res.data.awork[i - 1]
              adata.number = adata.number + "A"
              listAll.push(adata)
            }
          }
          else{
            let count = Math.ceil(Ccount / Acount)
            for(let i = 1; i <= Ccount; i++){
              if(i % count == 0){
                let adata = res.data.awork[i / count - 1]
                adata.number = adata.number + "A"
                listAll.push(adata)
              }
              let cdata = res.data.cwork[i - 1]
              cdata.number = cdata.number + "C"
              listAll.push(cdata)
            }
          }
          
          this.x_data_1 = listAll.map(item =>{
            return item.number
          })
          this.access_time_1 = listAll.map(item =>{
            return item.accessTime
          })
          this.work_time_1 = listAll.map(item =>{
            return item.workTime
          })
          this.chart_key1 ++;
          this.isChecked = false
        } else {
          this.$message.error("error");
        }
      })
    },
    changeData2 () {
      let paramA = this.$refs.IntervalParamA2.get_param()
      let paramC = this.$refs.IntervalParamC2.get_param()
      let param = this.$refs.TimeParam.get_param()
      let valueA = Math.min(paramA.FH / param.A, paramA.FC / param.C, paramA.MO *30.4)
      let valueC = Math.min(paramC.FH / param.A, paramC.FC / param.C, paramC.MO *30.4)
      let bodyJson = {
        atime: {year: param.max, value: valueA},
	      ctime: {year: param.max, value: valueC}
      }
      updateWorkHourSchemeTwo(bodyJson, this.userId).then((res) => {
        console.log(res)
        if (res.status === 200) {
          console.log("200 ok")
          this.AaccessSum_2 = res.data.totalAAccessHour
          this.AworkSum_2 = res.data.totalAWorkHour
          this.ASum_2 = res.data.totalAHour
          this.CaccessSum_2 = res.data.totalCAccessHour
          this.CworkSum_2 = res.data.totalCWorkHour
          this.CSum_2 = res.data.totalCHour

          let listAll = []
          let Acount = res.data.awork.length
          let Ccount = res.data.cwork.length
          this.AworkLength2 = Acount
          this.CworkLength2 = Ccount

          if(Acount != 0) {this.AValue2 = res.data.totalAHour / Acount} else this.AValue2 = 0
          if(Ccount != 0) {this.CValue2 = res.data.totalCHour / Ccount} else this.CValue2 = 0
          if(Acount + Ccount != 0) {this.SumValue2 = (res.data.totalAHour + res.data.totalCHour) / (Acount + Ccount)} else this.SumValue2 = 0


          if(Acount >= Ccount){
            let count = Math.ceil(Acount / Ccount)
            for(let i = 1; i <= Acount; i++){
              if(i % count == 0){
                let cdata = res.data.cwork[i / count - 1]
                cdata.number = cdata.number + "C"
                listAll.push(cdata)
              }
              let adata = res.data.awork[i - 1]
              adata.number = adata.number + "A"
              listAll.push(adata)
            }
          }
          else{
            let count = Math.ceil(Ccount / Acount)
            for(let i = 1; i <= Ccount; i++){
              if(i % count == 0){
                let adata = res.data.awork[i / count - 1]
                adata.number = adata.number + "A"
                listAll.push(adata)
              }
              let cdata = res.data.cwork[i - 1]
              cdata.number = cdata.number + "C"
              listAll.push(cdata)
            }
          }

          this.x_data_2 = listAll.map(item =>{
            return item.number
          })
          this.access_time_2 = listAll.map(item =>{
            return item.accessTime
          })
          this.work_time_2 = listAll.map(item =>{
            return item.workTime
          })
          this.chart_key2 ++;
          this.isChecked2 = false
        } else {
          this.$message.error("error");
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.Interval-container{
  margin: 50px 10% 0 10%;
  padding: 0 0 20px 0;
  width: 1200px;
  border-bottom: 2px solid #aaaaaa;
}
.Interval-title{
  display: flex;
  font-weight: bold;
}
.Interval-title .el-switch{
  margin: 0 0 0 55%;
}
.Interval-main{
  display: flex;
  justify-content: space-between;
}
.Interval-main div:nth-child(3){
  margin-left: 150px;
}

.time-container{
  margin: 50px 10% 0 10%;
  padding: 0 0 20px 0;
  width: 1200px;
  border-bottom: 2px solid #aaaaaa;
}

.buttons-container{
  display: flex;
  flex-flow: row nowrap;
  justify-content: flex-end;
  margin: 30px 10% 50px 10%;
  width: 1200px;
}

.table-container{
  padding: 20px 0 20px 0;
}
.bar-chart{
  margin: 0 10% 0 10%;
}
.table-item table{
  width: 75%;
  margin: 0 10% 0 10%;
  max-width: 1200px;
}
.el-button{
  background-color: #bdbdbd;
  color: black;
  border: black;
}
.el-dropdown{
  margin-left: 10px;
}
.table-item{
  width: 100%;
}
.table-item th{
  background-color: #dedede;
}
.table-item td{
  padding-left: 5px;
}
</style>
